package cn.hsa.elec.arc.storagemanage.bo.impl;

import cn.hsa.elec.arc.casemanage.dao.CaseManageDAO;
import cn.hsa.elec.arc.common.ArcNames;
import cn.hsa.elec.arc.common.util.ElecArcUtil;
import cn.hsa.elec.arc.common.util.IdGeneratorUtil;
import cn.hsa.elec.arc.storagemanage.bo.StorageManagerBO;
import cn.hsa.elec.arc.storagemanage.dao.*;
import cn.hsa.elec.arc.storagemanage.entity.ElecascOptionsDDO;
import cn.hsa.elec.arc.storagemanage.entity.ElecascRoomColDDO;
import cn.hsa.elec.arc.storagemanage.entity.ElecascRoomDDO;
import cn.hsa.elec.arc.storagemanage.entity.ElecascRoomFrameDDO;
import cn.hsa.hsaf.core.framework.util.CurrentUser;
import cn.hsa.hsaf.core.framework.web.exception.AppException;
import cn.hsa.hsaf.idgenerator.IdGenerator;
import com.dareway.framework.ddaaspect.InDdaEnv;
import com.dareway.framework.util.DataObject;
import com.dareway.framework.util.DataStore;
import com.dareway.framework.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author fengqi
 * @date 2022/12/19
 */
@Component("storageManagerBO")
public class StorageManagerBOImpl implements StorageManagerBO {

    @Autowired
    private IdGenerator idGenerator;
    @Resource
    private ElecascRoomDDOMapper elecascRoomDDOMapper;
    @Resource
    private ElecascOptionsDDOMapper elecascOptionsDDOMapper;
    @Resource
    private ElecascRoomFrameDDOMapper elecascRoomFrameDDOMapper;
    @Resource
    private ElecascRoomColDDOMapper elecascRoomColDDOMapper;
    @Resource
    private ElecaseInoutDAO elecaseInoutDAO;
    @Resource
    private ElecaseInoutInfoDAO elecaseInoutInfoDAO;
    @Resource
    private CaseManageDAO caseManageDAO;


    /**
     * 方法简介.库房查询
     *
     * @author fengqi
     * @date 2022/12/19
     */
    @Override
    @InDdaEnv
    public DataObject queryStorageManager(DataObject para) throws Exception {
        String arch_optins_id = "";//档案机构编号
        DataStore archds = ElecArcUtil.queryCrtOptins();
        if (archds.rowCount() > 0) {
            arch_optins_id = archds.getString(0, "arch_optins_id");
        } else {
            throw new com.dareway.framework.exception.AppException("人员未关联档案机构，请联系管理员维护人员信息");
        }

        //1.接入参
        String room_mc = para.getString("room_mc", "");
        room_mc = "%" + room_mc + "%";

        //2.获取室信息
        DataObject pdo = DataObject.getInstance();
        pdo.put("room_mc", room_mc);
        pdo.put("arch_optins_id", arch_optins_id);
        DataStore roomInfo = elecascRoomDDOMapper.getRoomInfo(pdo);

        for (int i = 0; i < roomInfo.rowCount(); i++) {
            String room_no = roomInfo.getString(i, "room_no");
            ElecascRoomDDO elecascRoomDDO = new ElecascRoomDDO();
            elecascRoomDDO.setRoomNo(room_no);

            DataStore getRoomframeno = elecascRoomDDOMapper.getRoomframeno(elecascRoomDDO);
            Double sjs = 0.00;
            if (getRoomframeno.rowCount() > 0) {
                sjs = getRoomframeno.getDouble(0, "sjs");
            }

            elecascRoomDDO.setRoomNo(room_no);
            DataStore getArchsaveno = elecascRoomDDOMapper.getArchsaveno(elecascRoomDDO);
            Double cfsl = 0.00;
            if (getArchsaveno.rowCount() > 0) {
                cfsl = getArchsaveno.getDouble(0, "cfsl");
            }
            roomInfo.put(i, "room_no", room_no);
            roomInfo.put(i, "sjs", sjs);
            roomInfo.put(i, "cfsl", cfsl);
        }

        //3.返回室信息
        DataObject rdo = DataObject.getInstance();
        rdo.put("roomInfo", roomInfo);

        return rdo;
    }

    /**
     * 方法简介.库房查询-查询档案机构信息
     *
     * @author fengqi
     * @date 2022/12/24
     */
    @Override
    @InDdaEnv
    public DataObject queryArchOptins(DataObject para) throws Exception {

        //1.接入参
        String arch_optins_id = para.getString("key", "");
        String arch_optins_name = para.getString("key", "");
        arch_optins_name = "%" + arch_optins_name + "%";

        DataObject pdo = DataObject.getInstance();
        pdo.put("arch_optins_id", arch_optins_id);
        pdo.put("arch_optins_name", arch_optins_name);

        //2.获取机构档案信息
        DataStore archInfo = elecascOptionsDDOMapper.getArchInfo(pdo);

        //3.返回机构档案信息
        DataObject rdo = DataObject.getInstance();
        rdo.put("archInfo", archInfo);

        return rdo;
    }

    /**
     * 方法简介.库房-室查询
     *
     * @author fengqi
     * @date 2022/12/30
     */
    @Override
    @InDdaEnv

    public DataObject queryRoom(DataObject para) throws Exception {
        String arch_optins_id = "";//档案机构编号
        DataStore archds = ElecArcUtil.queryCrtOptins();
        if (archds.rowCount() > 0) {
            arch_optins_id = archds.getString(0, "arch_optins_id");
        } else {
            throw new com.dareway.framework.exception.AppException("人员未关联档案机构，请联系管理员维护人员信息");
        }

        //1.接入参
        String room_no = para.getString("room_no", "");

        DataObject pdo = DataObject.getInstance();
        pdo.put("room_no", room_no);
        pdo.put("arch_optins_id", arch_optins_id);

        //2.获取室信息
        DataStore roomInfo = elecascRoomDDOMapper.getRoomInfo(pdo);

        //3.返回室信息
        DataObject rdo = DataObject.getInstance();
        rdo.put("roomInfo", roomInfo);

        return rdo;
    }


    /**
     * 方法简介.库房-架查询
     *
     * @author fengqi
     * @date 2022/12/28
     */
    @Override
    @InDdaEnv

    public DataObject queryFrame(DataObject para) throws Exception {

        //1.接入参
        String room_no = para.getString("room_no", "");
        String frame_no = para.getString("frame_no", "");

        DataObject pdo = DataObject.getInstance();
        pdo.put("room_no", room_no);
        pdo.put("frame_no", frame_no);

        //2.获取室信息
        DataStore frameInfo = elecascRoomFrameDDOMapper.getFrameInfo(pdo);

        //3.返回室信息
        DataObject rdo = DataObject.getInstance();
        rdo.put("frameInfo", frameInfo);

        return rdo;
    }

    /**
     * 方法简介.库房-架位置信息查询
     *
     * @author fengqi
     * @date 2022/12/28
     */
    @Override
    @InDdaEnv

    public DataObject queryFrameLocInfo(DataObject para) throws Exception {
        //1.接入参：室编号、架编码、架序号。
        String room_no = para.getString("room_no");
        String frame_no = para.getString("frame_no");

        DataObject pdo = DataObject.getInstance();
        pdo.put("room_no", room_no);
        pdo.put("frame_no", frame_no);

        //2.获取库房-架位置信息
        DataStore frameLocInfo = elecascRoomColDDOMapper.getFrameLocInfo(pdo);

        //3.返回库房-架位置信息
        DataObject rdo = DataObject.getInstance();
        rdo.put("frameLocInfo", frameLocInfo);

        return rdo;
    }

    /**
     * 方法简介.新增库房管理信息（确认按钮）
     *
     * @author fengqi
     * @date 2022/12/19
     */
    @Override
    @InDdaEnv
    public DataObject addStorageManager(DataObject para) throws Exception {
        CurrentUser cu = ElecArcUtil.getUser();
        String arch_optins_id = "";//档案机构编号
        DataStore archds = ElecArcUtil.queryCrtOptins();
        if (archds.rowCount() > 0) {
            arch_optins_id = archds.getString(0, "arch_optins_id");
        } else {
            throw new com.dareway.framework.exception.AppException("人员未关联档案机构，请联系管理员维护人员信息");
        }

        //1.接入参
        String room_mc = para.getString("room_mc");
        String stroom_addr = para.getString("stroom_addr");
        String room_dscr = para.getString("room_dscr", "");

        //2.校验库房信息是否已经存在
        ElecascRoomDDO pdo = new ElecascRoomDDO();
        pdo.setRoomMc(room_mc);
        DataStore getRoomExist = elecascRoomDDOMapper.getRoomExist(pdo);

        if (getRoomExist.rowCount() > 0) {
            throw new AppException("库房信息已经存在！");
        }

        //3.把入参放到DDO中  维护创建人，经办人员信息
        ElecascRoomDDO elecascRoomDDO = new ElecascRoomDDO();
        elecascRoomDDO.setRoomMc(room_mc);
        elecascRoomDDO.setStroomAddr(stroom_addr);
        elecascRoomDDO.setRoomDscr(room_dscr);
        elecascRoomDDO.setRoomNo(IdGeneratorUtil.getStrId("ROOM_NO"));
        elecascRoomDDO.setArchOptinsId(arch_optins_id);
        elecascRoomDDO.setCrter(cu.getUserAcctID());
        String optins = archds.getString(0,"arch_optins_id");//经办机构
        elecascRoomDDO.setOptins(optins);
        elecascRoomDDO.setOpter(cu.getUserAcctID());
        elecascRoomDDO.setCrteOptins(optins);
        elecascRoomDDO.setPoolarea(cu.getPoolAreaCodg());

        int flag = elecascRoomDDOMapper.insertSelective(elecascRoomDDO);
        if (flag < 0) {
            throw new AppException("新增室信息失败");
        }

        //4.返回库房-档案机构名称
        return null;
    }

    /**
     * 方法简介.库房-架新增
     *
     * @author fengqi
     * @date 2022/12/24
     */
    @Override
    @InDdaEnv
    public DataObject addStorageFrame(DataObject para) throws Exception {
        CurrentUser cu = ElecArcUtil.getUser();

        //1.接入参
        String room_no = para.getString("room_no");
        String room_mc = "";
        String frame_no = IdGeneratorUtil.getStrId("FRAME_NO");
        String frame_name = para.getString("frame_name");
        String frame_sn = para.getString("frame_sn");
        int sec_num = para.getInt("sec_num");
        int lve_num = para.getInt("lve_num");
        int col_numr = sec_num * lve_num;
        String frame_dscr = para.getString("frame_dscr", "");
        int file_num = 0;
        int sec_sn = 0;
        int lve_sn = 0;
        int col_sn = 0;

        //2.校验架信息是否已经存在
        ElecascRoomFrameDDO pdo = new ElecascRoomFrameDDO();
        pdo.setFrameName(frame_name);
        pdo.setRoomNo(room_no);
        DataStore roomFrameExist = elecascRoomFrameDDOMapper.getRoomFrameExist(pdo);

        if (roomFrameExist.rowCount() > 0) {
            throw new AppException("库房信息已经存在！");
        }

        //3.把入参放到DDO中
        ElecascRoomFrameDDO elecascRoomFrameDDO = new ElecascRoomFrameDDO();
        elecascRoomFrameDDO.setRoomNo(room_no);
        elecascRoomFrameDDO.setFrameNo(frame_no);
        elecascRoomFrameDDO.setFrameName(frame_name);
        elecascRoomFrameDDO.setFrameSn(frame_sn);
        elecascRoomFrameDDO.setSecNum(sec_num);
        elecascRoomFrameDDO.setLveNum(lve_num);
        elecascRoomFrameDDO.setColNumr(col_numr);
        elecascRoomFrameDDO.setFrameDscr(frame_dscr);
        elecascRoomFrameDDO.setCrter(cu.getUserAcctID());
        DataStore archds = ElecArcUtil.queryCrtOptins();
        String optins = archds.getString(0,"arch_optins_id");//经办机构
        elecascRoomFrameDDO.setOptins(optins);
        elecascRoomFrameDDO.setOpter(cu.getUserAcctID());
        elecascRoomFrameDDO.setCrteOptins(optins);
        elecascRoomFrameDDO.setPoolarea(cu.getPoolAreaCodg());
        int flag = elecascRoomFrameDDOMapper.insertSelective(elecascRoomFrameDDO);
        if (flag < 0) {
            throw new AppException("新增架信息失败");
        }
        //每一列的栏序号唯一，且从1开始排序；栏的排序规则：按照自上而下，自左至右的顺序
        for (int j = 1; j <= lve_num; j++) {
            for (int c = 1; c <= sec_num; c++) {
                DataObject rdo = DataObject.getInstance();
                rdo.put("room_no", room_no);
                DataStore roomInfo = elecascRoomDDOMapper.getRoomInfo(rdo);
                if (roomInfo.rowCount() > 0) {
                    room_mc = roomInfo.getString(0, "room_mc");
                }
                lve_sn = j;
                sec_sn = c;
                col_sn = col_sn + 1;
                String save_loc_code = room_mc + "室-" + frame_name + "架-" + sec_sn + "节-" + lve_sn + "层";
                String col_no = IdGeneratorUtil.getStrId("COL_NO");
                ElecascRoomColDDO elecascRoomColDDO = new ElecascRoomColDDO();
                elecascRoomColDDO.setFrameNo(frame_no);
                elecascRoomColDDO.setColNo(col_no);
                elecascRoomColDDO.setColSn(col_sn);
                elecascRoomColDDO.setSecSn(sec_sn);
                elecascRoomColDDO.setLveSn(lve_sn);
                elecascRoomColDDO.setSaveLocCode(save_loc_code);
                elecascRoomColDDO.setFileNum(file_num);
                elecascRoomColDDO.setCrter(cu.getUserAcctID());
                optins = archds.getString(0,"arch_optins_id");//经办机构
                elecascRoomColDDO.setOptins(optins);
                elecascRoomColDDO.setOpter(cu.getUserAcctID());
                elecascRoomColDDO.setCrteOptins(optins);
                elecascRoomColDDO.setPoolarea(cu.getPoolAreaCodg());
                int newflag = elecascRoomColDDOMapper.insertSelective(elecascRoomColDDO);
                if (newflag < 0) {
                    throw new AppException("新增栏信息失败");
                }
            }
        }
        return null;
    }

    /**
     * 方法简介.修改库房管理信息
     *
     * @author fengqi
     * @date 2022/12/19
     */
    @Override
    @InDdaEnv
    public DataObject updateStorageManager(DataObject para) throws Exception {

        CurrentUser cu = ElecArcUtil.getUser();
        String arch_optins_id = "";//档案机构编号
        DataStore archds = ElecArcUtil.queryCrtOptins();
        if (archds.rowCount() > 0) {
            arch_optins_id = archds.getString(0, "arch_optins_id");
        } else {
            throw new com.dareway.framework.exception.AppException("人员未关联档案机构，请联系管理员维护人员信息");
        }

        //1.接入参
        String room_mc = para.getString("room_mc", "");
        String ds_room_mc = "";
        String stroom_addr = para.getString("stroom_addr", "");
        String room_dscr = para.getString("room_dscr", "");
        String room_no = para.getString("room_no", "");

        //2.查询该室是否存在
        ElecascRoomDDO pdo = new ElecascRoomDDO();
        pdo.setRoomNo(room_no);
        DataStore roomExist = elecascRoomDDOMapper.getRoomExist(pdo);
        if (roomExist.rowCount() < 1) {
            throw new AppException("该库房不存在！");
        }

        //3.判断是否修改库房名称，若修改，需校验修改后的库房名称是否已存在
        DataObject tdo = DataObject.getInstance();
        tdo.put("room_no", room_no);
        DataStore roomInfo = elecascRoomDDOMapper.getRoomInfo(tdo);
        if (roomInfo.rowCount() > 0) {
            ds_room_mc = roomInfo.getString(0, "room_mc");
        }

        if (!room_mc.equals(ds_room_mc)) {
            ElecascRoomDDO rdo = new ElecascRoomDDO();
            rdo.setRoomMc(room_mc);
            roomExist = elecascRoomDDOMapper.getRoomExist(rdo);

            if (roomExist.rowCount() > 0) {
                throw new AppException("库房信息已经存在");
            }
        }

        //4.返回室信息
        ElecascRoomDDO elecascRoomDDO = new ElecascRoomDDO();
        elecascRoomDDO.setRoomMc(room_mc);
        elecascRoomDDO.setStroomAddr(stroom_addr);
        elecascRoomDDO.setRoomDscr(room_dscr);
        elecascRoomDDO.setRoomNo(room_no);
        elecascRoomDDO.setCrter(cu.getUserAcctID());
        String optins = archds.getString(0,"arch_optins_id");//经办机构
        elecascRoomDDO.setOptins(optins);
        elecascRoomDDO.setOpter(cu.getUserAcctID());
        elecascRoomDDO.setCrteOptins(optins);
        elecascRoomDDO.setPoolarea(cu.getPoolAreaCodg());
        elecascRoomDDO.setArchOptinsId(arch_optins_id);
        int flag = elecascRoomDDOMapper.updateRoomInfo(elecascRoomDDO);
        if (flag < 0) {
            throw new AppException("修改室信息失败");
        }

        return null;
    }

    /**
     * 方法简介.库房-架修改
     *
     * @author fengqi
     * @date 2022/12/24
     */
    @Override
    @InDdaEnv
    public DataObject updateStorageFrame(DataObject para) throws Exception {
        CurrentUser cu = ElecArcUtil.getUser();

        //1.接入参
        String frame_name = para.getString("frame_name");
        String ds_frame_name = "";
        String frame_sn = para.getString("frame_sn");
        String frame_dscr = para.getString("frame_dscr", "");
        String frame_no = para.getString("frame_no");

        //2.查询该列是否存在
        ElecascRoomFrameDDO pdo = new ElecascRoomFrameDDO();
        pdo.setFrameNo(frame_no);
        DataStore roomFrameExist = elecascRoomFrameDDOMapper.getRoomFrameExist(pdo);
        if (roomFrameExist.rowCount() < 1) {
            throw new AppException("该架不存在！");
        }

        //3.判断是否修改架名称，若修改，需校验修改后的架名称是否已存在
        DataObject tdo = DataObject.getInstance();
        tdo.put("frame_no", frame_no);
        DataStore frameInfo = elecascRoomFrameDDOMapper.getFrameInfo(tdo);
        if (frameInfo.rowCount() > 0) {
            ds_frame_name = frameInfo.getString(0, "frame_name");
        }

        if (!frame_name.equals(ds_frame_name)) {
            ElecascRoomFrameDDO rdo = new ElecascRoomFrameDDO();
            rdo.setFrameName(frame_name);
            roomFrameExist = elecascRoomFrameDDOMapper.getRoomFrameExist(rdo);
            if (roomFrameExist.rowCount() > 0) {
                throw new AppException("架名称已经存在！");
            }
        }

        //4.返回室信息
        ElecascRoomFrameDDO elecascRoomFrameDDO = new ElecascRoomFrameDDO();
        elecascRoomFrameDDO.setFrameName(frame_name);
        elecascRoomFrameDDO.setFrameSn(frame_sn);
        elecascRoomFrameDDO.setFrameDscr(frame_dscr);
        elecascRoomFrameDDO.setFrameNo(frame_no);
        elecascRoomFrameDDO.setCrter(cu.getUserAcctID());
        DataStore archds = ElecArcUtil.queryCrtOptins();
        String optins = archds.getString(0,"arch_optins_id");//创建机构
        elecascRoomFrameDDO.setOptins(optins);
        elecascRoomFrameDDO.setOpter(cu.getUserAcctID());
        elecascRoomFrameDDO.setCrteOptins(optins);
        elecascRoomFrameDDO.setPoolarea(cu.getPoolAreaCodg());

        int flag = elecascRoomFrameDDOMapper.updateRoomFrameInfo(elecascRoomFrameDDO);
        if (flag < 0) {
            throw new AppException("修改架信息失败");
        }
        return null;
    }

    /**
     * 方法简介.删除库房管理信息
     *
     * @author fengqi
     * @date 2022/12/19
     */
    @Override
    @InDdaEnv
    public DataObject deleteStorageManager(DataObject para) throws Exception {

        //1.接入参
        String room_no = para.getString("room_no");
        String frame_no = "";
        String col_no = "";
        int listsn = 0;

        //2.根据室编码获取架信息,循环架信息获取架编码、架文件数量
        DataObject pdo = DataObject.getInstance();
        pdo.put("room_no", room_no);
        DataStore frameInfo = elecascRoomFrameDDOMapper.getFrameInfo(pdo);

        for (int i = 0; i < frameInfo.rowCount(); i++) {
            frame_no = frameInfo.getString(i, "frame_no");
            listsn = frameInfo.getInt(i, "listsn");
            if (listsn != 0) {
                throw new AppException("库房下存在文件，请先删除库房内文件");
            }

            //3.根据架编码循环架获取栏编码
            DataObject rdo = DataObject.getInstance();
            rdo.put("frame_no", frame_no);
            DataStore getColInfo = elecascRoomColDDOMapper.getColInfo(rdo);

            for (int k = 0; k < getColInfo.rowCount(); k++) {
                col_no = getColInfo.getString(k, "col_no");

                //4.删除栏信息
                ElecascRoomColDDO elecascRoomColDDO = new ElecascRoomColDDO();
                elecascRoomColDDO.setColNo(col_no);
                int secflag = elecascRoomColDDOMapper.deleteRoomColInfo(elecascRoomColDDO);
                if (secflag < 0) {
                    throw new AppException("删除栏信息失败");
                }
            }
            //5.删除架信息
            ElecascRoomFrameDDO elecascRoomFrameDDO = new ElecascRoomFrameDDO();
            elecascRoomFrameDDO.setFrameNo(frame_no);
            int newflag = elecascRoomFrameDDOMapper.deleteRoomFrameInfo(elecascRoomFrameDDO);
            if (newflag < 0) {
                throw new AppException("删除架信息失败");
            }
        }
        //6.删除室信息
        ElecascRoomDDO elecascRoomDDO = new ElecascRoomDDO();
        elecascRoomDDO.setRoomNo(room_no);
        int flag = elecascRoomDDOMapper.deleteRoomInfo(elecascRoomDDO);
        if (flag < 0) {
            throw new AppException("删除室信息失败");
        }
        return null;
    }

    /**
     * 方法简介.库房-架删除
     *
     * @author fengqi
     * @date 2022/12/24
     */
    @Override
    @InDdaEnv
    public DataObject deleteStorageFrame(DataObject para) throws Exception {

        //1.接入参
        String frame_no = para.getString("frame_no", "");

        //2.根据架编码查询栏信息
        DataObject tdo = DataObject.getInstance();
        tdo.put("frame_no", frame_no);
        DataStore getColInfo = elecascRoomColDDOMapper.getColInfo(tdo);

        //3.循环栏信息获取文件数量，大于0时报错
        for (int k = 0; k < getColInfo.rowCount(); k++) {
            int file_num = getColInfo.getInt(k, "file_num");
            String col_no = getColInfo.getString(k, "col_no");
            if (file_num != 0) {
                throw new AppException("该架下存在文件，请先删除架内文件");
            }

            //4.删除栏信息
            ElecascRoomColDDO elecascRoomColDDO = new ElecascRoomColDDO();
            elecascRoomColDDO.setColNo(col_no);
            int newflag = elecascRoomColDDOMapper.deleteRoomColInfo(elecascRoomColDDO);
            if (newflag < 0) {
                throw new AppException("删除栏信息失败");
            }
        }
        //5.删除架信息
        ElecascRoomFrameDDO elecascRoomFrameDDO = new ElecascRoomFrameDDO();
        elecascRoomFrameDDO.setFrameNo(frame_no);
        int flag = elecascRoomFrameDDOMapper.deleteRoomFrameInfo(elecascRoomFrameDDO);
        if (flag < 0) {
            throw new AppException("删除架信息失败");
        }
        return null;
    }


    /**
     * 方法简介.查询档案机构名称
     *
     * @author fengqi
     * @date 2022/1/6
     */
    @Override
    @InDdaEnv
    public DataObject queryArchOptionName(DataObject para) throws Exception {

        //1.根据档案机构id返回查询信息
        ElecascOptionsDDO elecascOptionsDDO = new ElecascOptionsDDO();
        CurrentUser cu = ElecArcUtil.getUser();
        DataStore archds = ElecArcUtil.queryCrtOptins();
        String deptid = archds.getString(0,"arch_optins_id");//档案机构编号
        elecascOptionsDDO.setArchOptinsId(deptid);

        DataStore queryArchOptionName = elecascOptionsDDOMapper.queryArchOptionName(elecascOptionsDDO);
        if (queryArchOptionName.rowCount() < 1) {
            throw new AppException("查询库房-档案机构名称失败");
        }

        //2.返回档案机构id、档案机构名称
        DataObject rdo = DataObject.getInstance();
        rdo.put("queryArchOptionName", queryArchOptionName);
        return rdo;
    }

    /**
     * 方法简介.档案出入库情况查询
     *
     * @author lc
     * @date 2023/1/11
     */
    @Override
    @InDdaEnv
    public DataObject queryInOrOutStorageInfo(DataObject para) throws Exception {
        CurrentUser cu = ElecArcUtil.getUser();
        Date qssj = para.getStringDate("qssj","yyyy-mm-dd", null);//起始时间
        Date zzsj = para.getStringDate("zzsj", "yyyy-mm-dd",  null);//终止时间
        String room_no = para.getString("room_no", "");//室编号（库房id）
        String inout_type = para.getString("inout_type", "");//出入库类型
        String inout_no = para.getString("inout_no", "");//出入库编号
        String delv_name = para.getString("delv_name", "");//移交人
        String recer_name = para.getString("recer_name", "");//接收人
        String arch_optins_id = "";//档案机构编号
        DataStore archds = ElecArcUtil.queryCrtOptins();
        if (archds.rowCount() > 0) {
            arch_optins_id = archds.getString(0, "arch_optins_id");
        } else {
            throw new com.dareway.framework.exception.AppException("人员未关联档案机构，请联系管理员维护人员信息");
        }
        DataObject pdo = DataObject.getInstance();
        pdo.put("arch_optins_id", arch_optins_id);
        pdo.put("inout_type", inout_type);
        pdo.put("room_no", room_no);
        pdo.put("qssj", qssj);
        pdo.put("zzsj", zzsj);
        pdo.put("inout_no", inout_no);
        pdo.put("delv_name",  delv_name);
        pdo.put("recer_name", recer_name);
        DataStore ds = DataStore.getInstance();
        try {
            ds = elecaseInoutDAO.queryInOrOutStorageInfo(pdo);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }

        DataObject vdo = DataObject.getInstance();
        vdo.put("ds", ds);
        return vdo;
    }

    /**
     * 方法简介.入库登记
     *
     * @author lc
     * @date 2023/1/11
     */
    @Override
    @InDdaEnv
    public DataObject addRegStockIn(DataObject para) throws Exception {
        CurrentUser cu = ElecArcUtil.getUser();
        String inout_no = IdGeneratorUtil.getFixedLenId("INOUT_NO", 24);//出入库编号
        Date inout_dat = para.getDate("inout_dat", DateUtil.getCurrentDate());
        String inout_type = para.getString("inout_type", ArcNames.INOUT_TYPE.STOIN);//出入库类型
//        String room_no = para.getString("room_no", "");//室编号（库房id）
        String delv_name = para.getString("delv_name", cu.getName());//移交人姓名
//        String recer_id = para.getString("recer_id", "");//接收人
        String recer_name = para.getString("recer_name", cu.getName());//接收人姓名
        String inout_rea = para.getString("inout_rea", "");//入库原因
        String opter_id = cu.getUserAcctID();//经办人id
        String opter_name = cu.getName();//经办人姓名
        String arch_optins_id = "";//档案机构编号
        DataStore archds = ElecArcUtil.queryCrtOptins();
        if (archds.rowCount() > 0) {
            arch_optins_id = archds.getString(0, "arch_optins_id");
        } else {
            throw new com.dareway.framework.exception.AppException("人员未关联档案机构，请联系管理员维护人员信息");
        }
        DataStore case_id_ds = para.getDataStore("case_id_str", DataStore.getInstance());
        int total_file_num = case_id_ds.rowCount();
        if (total_file_num == 0) {
            throw new AppException("未选择要入库的档案，请检查！");
        }
//        String case_id_str = para.getString("case_id_str", "");//件id字符串（以逗号分隔）
//
//        String[] caseidList = case_id_str.split(",|，");
//        int total_file_num = caseidList.length;
        DataStore infods = DataStore.getInstance();
        String crter = cu.getUserAcctID();
        String poolarea = cu.getAdmDvs();
        String opter = cu.getName();
        Date opt_time = new Date();
        String optins = archds.getString(0,"arch_optins_id");//经办机构
        String case_id = "";
        String room_no = "";
        DataStore ds = DataStore.getInstance();
        for (int i = 0; i < total_file_num; i++) {
//            case_id = caseidList[i];
            case_id = case_id_ds.getString(i, "case_id");
            infods.addRow();
            infods.put(infods.rowCount() - 1, "case_id", case_id);
            infods.put(infods.rowCount() - 1, "inout_no", inout_no);
            infods.put(infods.rowCount() - 1, "case_xh", "");
            infods.put(infods.rowCount() - 1, "crter", crter);
            infods.put(infods.rowCount() - 1, "poolarea", poolarea);

            infods.put(infods.rowCount() - 1, "opter", opter);
            infods.put(infods.rowCount() - 1, "opt_time", opt_time);
            infods.put(infods.rowCount() - 1, "optins", optins);
            infods.put(infods.rowCount() - 1, "crte_optins", optins);
            infods.put(infods.rowCount() - 1, "inout_type", inout_type);

            room_no = case_id_ds.getString(i, "room_no");
            int find = ds.find("room_no == " + room_no);
            if (find < 0) {
                ds.addRow();
                ds.put(ds.rowCount() - 1,"inout_no", inout_no);
                ds.put(ds.rowCount() - 1,"inout_type", inout_type);
                ds.put(ds.rowCount() - 1,"inout_dat", inout_dat);
                ds.put(ds.rowCount() - 1, "room_no", room_no);
                ds.put(ds.rowCount() - 1,"arch_optins_id", arch_optins_id);

                ds.put(ds.rowCount() - 1,"total_file_num", total_file_num);
                ds.put(ds.rowCount() - 1,"opter_name", opter_name);
                ds.put(ds.rowCount() - 1,"opter_id", opter_id);
                ds.put(ds.rowCount() - 1,"recer_name", recer_name);

                ds.put(ds.rowCount() - 1,"inout_rea", inout_rea);
                ds.put(ds.rowCount() - 1,"crter", crter);
                ds.put(ds.rowCount() - 1,"poolarea", poolarea);
                ds.put(ds.rowCount() - 1,"opter", opter);
                ds.put(ds.rowCount() - 1,"opt_time", opt_time);

                ds.put(ds.rowCount() - 1,"optins", optins);
                ds.put(ds.rowCount() - 1,"crte_optins", optins);
                ds.put(ds.rowCount() - 1,"delv_name", delv_name);
            }
        }
        infods.setTypeList("opt_time:d");

//        DataObject pdo = DataObject.getInstance();
//        pdo.put("inout_no", inout_no);
//        pdo.put("inout_type", inout_type);
//        pdo.put("inout_dat", inout_dat);
//        pdo.put("room_no", room_no);
//        pdo.put("arch_optins_id", arch_optins_id);
//
//        pdo.put("total_file_num", total_file_num);
//        pdo.put("opter_name", opter_name);
//        pdo.put("opter_id", opter_id);
////        pdo.put("recer_id", recer_id);
//        pdo.put("recer_name", recer_name);
//
//        pdo.put("inout_rea", inout_rea);
//        pdo.put("crter", crter);
//        pdo.put("poolarea", poolarea);
//        pdo.put("opter", opter);
//        pdo.put("opt_time", opt_time);
//
//        pdo.put("optins", optins);
//        pdo.put("crte_optins", crte_optins);
//        pdo.put("delv_name", delv_name);

        try {
            elecaseInoutDAO.insertBatchElecaseInout(ds);
            elecaseInoutInfoDAO.insertBatchElecaseInoutInfo(infods);
            caseManageDAO.updateBatchCaseInoutType(infods);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        DataObject vdo = DataObject.getInstance();
        vdo.put("message", "入库成功");
        return vdo;
    }

    /**
     * 方法简介.出库登记
     *
     * @author lc
     * @date 2023/1/11
     */
    @Override
    @InDdaEnv
    public DataObject addRegStockOut(DataObject para) throws Exception {
        CurrentUser cu = ElecArcUtil.getUser();
        String arch_optins_id = "";//档案机构编号
        DataStore archds = ElecArcUtil.queryCrtOptins();
        if (archds.rowCount() > 0) {
            arch_optins_id = archds.getString(0, "arch_optins_id");
        } else {
            throw new com.dareway.framework.exception.AppException("人员未关联档案机构，请联系管理员维护人员信息");
        }
        String inout_no = IdGeneratorUtil.getFixedLenId("INOUT_NO", 24);//出入库编号
        Date inout_dat = para.getDate("inout_dat", DateUtil.getCurrentDate());
        String inout_type = para.getString("inout_type", ArcNames.INOUT_TYPE.STOOUT);//出入库类型
//        String room_no = para.getString("room_no", "");//室编号（库房id）
        String delv_name = para.getString("delv_name", "");//移交人姓名
//        String recer_id = para.getString("recer_id", "");//接收人
        String recer_name = para.getString("recer_name", "");//接收人姓名
        String inout_rea = para.getString("inout_rea", "");//入库原因
        String opter_id = cu.getUserAcctID();//经办人id
        String opter_name = cu.getName();//经办人姓名
        DataStore case_id_ds = para.getDataStore("case_id_str", DataStore.getInstance());
        int total_file_num = case_id_ds.rowCount();
        if (total_file_num == 0) {
            throw new AppException("未选择要出库的档案，请检查！");
        }
//        String case_id_str = para.getString("case_id_str", "");//件id字符串（以逗号分隔）
//        String[] caseidList = case_id_str.split(",|，");
//        int total_file_num = caseidList.length;
        DataStore infods = DataStore.getInstance();
        String crter = cu.getUserAcctID();
        String poolarea = cu.getAdmDvs();
        String opter = cu.getName();
        Date opt_time = new Date();
        String optins = archds.getString(0,"arch_optins_id");//经办机构
        String case_id = "";
        String room_no = "";
        DataStore ds = DataStore.getInstance();
        for (int i = 0; i < total_file_num; i++) {
//            case_id = caseidList[i];
            case_id = case_id_ds.getString(i, "case_id");
            infods.addRow();
            infods.put(infods.rowCount() - 1, "case_id", case_id);
            infods.put(infods.rowCount() - 1, "inout_no", inout_no);
            infods.put(infods.rowCount() - 1, "case_xh", "");
            infods.put(infods.rowCount() - 1, "crter", crter);
            infods.put(infods.rowCount() - 1, "poolarea", poolarea);

            infods.put(infods.rowCount() - 1, "opter", opter);
            infods.put(infods.rowCount() - 1, "opt_time", opt_time);
            infods.put(infods.rowCount() - 1, "optins", optins);
            infods.put(infods.rowCount() - 1, "crte_optins", optins);
            infods.put(infods.rowCount() - 1, "inout_type", inout_type);

            room_no = case_id_ds.getString(i, "room_no");
            int find = ds.find("room_no == " + room_no);
            if (find < 0) {
                ds.addRow();
                ds.put(ds.rowCount() - 1,"inout_no", inout_no);
                ds.put(ds.rowCount() - 1,"inout_type", inout_type);
                ds.put(ds.rowCount() - 1,"inout_dat", inout_dat);
                ds.put(ds.rowCount() - 1, "room_no", room_no);
                ds.put(ds.rowCount() - 1,"arch_optins_id", arch_optins_id);

                ds.put(ds.rowCount() - 1,"total_file_num", total_file_num);
                ds.put(ds.rowCount() - 1,"opter_name", opter_name);
                ds.put(ds.rowCount() - 1,"opter_id", opter_id);
                ds.put(ds.rowCount() - 1,"recer_name", recer_name);

                ds.put(ds.rowCount() - 1,"inout_rea", inout_rea);
                ds.put(ds.rowCount() - 1,"crter", crter);
                ds.put(ds.rowCount() - 1,"poolarea", poolarea);
                ds.put(ds.rowCount() - 1,"opter", opter);
                ds.put(ds.rowCount() - 1,"opt_time", opt_time);

                ds.put(ds.rowCount() - 1,"optins", optins);
                ds.put(ds.rowCount() - 1,"crte_optins", optins);
                ds.put(ds.rowCount() - 1,"delv_name", delv_name);
            }
        }
        infods.setTypeList("opt_time:d");

//        DataObject pdo = DataObject.getInstance();
//        pdo.put("inout_no", inout_no);
//        pdo.put("inout_type", inout_type);
//        pdo.put("inout_dat", inout_dat);
//        pdo.put("room_no", room_no);
//        pdo.put("arch_optins_id", arch_optins_id);
//
//        pdo.put("total_file_num", total_file_num);
//        pdo.put("opter_name", opter_name);
//        pdo.put("opter_id", opter_id);
////        pdo.put("recer_id", recer_id);
//        pdo.put("recer_name", recer_name);
//
//        pdo.put("inout_rea", inout_rea);
//        pdo.put("crter", crter);
//        pdo.put("poolarea", poolarea);
//        pdo.put("opter", opter);
//        pdo.put("opt_time", opt_time);
//
//        pdo.put("optins", optins);
//        pdo.put("crte_optins", crte_optins);
//        pdo.put("delv_name", delv_name);

        try {
            elecaseInoutDAO.insertBatchElecaseInout(ds);
            elecaseInoutInfoDAO.insertBatchElecaseInoutInfo(infods);
            caseManageDAO.updateBatchCaseInoutType(infods);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        DataObject vdo = DataObject.getInstance();
        vdo.put("message", "出库成功");
        return vdo;
    }

    /**
     * 方法简介.档案出入库详情查询
     *
     * @author lc
     * @date 2023/1/11
     */
    @Override
    @InDdaEnv
    public DataObject queryInOrOutStorageDetailInfo(DataObject para) throws Exception {
        String inout_no = para.getString("inout_no", "");//出入库编号
        if ("".equals(inout_no) || inout_no == null) {
            throw new AppException("未传入出入库编码，请检查！");
        }
        DataStore ds = elecaseInoutInfoDAO.queryInOutDetailInfoByNo(inout_no);
        DataObject vdo = DataObject.getInstance();
        vdo.put("ds", ds);
        return vdo;
    }
}
